package com.lin.services.impl;

import com.lin.dao.CategoryDao;
import com.lin.dao.OrderDao;
import com.lin.pojo.Category;
import com.lin.pojo.WaresOrder;
import com.lin.services.DamageReportService;
import com.lin.utils.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName DamageReportServiceImpl
 * Package com.lin.services.impl
 *
 * @author hellcat
 * Date 2020/3/18
 * Description:
 * 🐾
 */
@Service
public class DamageReportServiceImpl implements DamageReportService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private CategoryDao categoryDao;

    @Override
    public List<WaresOrder> getDamageReport(String searchDate, String searchWaresId) {
        try {
            String[] split = searchDate.trim().split(" - ");
            return orderDao.getOrderWaresByType(2, split[0], split[1], searchWaresId, ShiroUtil.getUser().getStoreId());
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public Map<String, Object> getChartsData(String date) {
        Map<String, Object> map = new HashMap<>();
        try {
            String[] split = date.split(" - ");
//            单子总价
            List<WaresOrder> list = orderDao.getTotalPriceByDate(
                    2, split[0], split[1], ShiroUtil.getUser().getStoreId());
            double moneyTotal = 0.0;
            for (WaresOrder waresOrder : list) {
                moneyTotal = moneyTotal + waresOrder.getUnitPrice();
            }

//            饼图
            List<Category> categories = categoryDao.listCategory1(ShiroUtil.getUser().getStoreId());
            List<Map<String, Object>> pieList = new ArrayList<>();
            for (Category category : categories) {
                double total = orderDao.getCostByCategoryId(category.getCategoryId(), 2, split[0], split[1],
                        ShiroUtil.getUser().getStoreId());
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("value", total);
                dataMap.put("name", category.getCategoryName());
                pieList.add(dataMap);
            }
            map.put("code", true);
            map.put("pieData", pieList);
            map.put("damageReportCard1", list.size());
            map.put("damageReportCard2", moneyTotal);
        } catch (Exception e) {
            map.clear();
            map.put("code", false);
            map.put("msg", "数据加载失败");
        }
        return map;
    }
}
